package com.pactera.asmp.server.restfullapi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pactera.asmp.server.common.constants.PermissionCode;
import com.pactera.asmp.server.common.constants.ResultCode;
import com.pactera.asmp.server.common.logprint.utils.StringUtils;
import com.pactera.asmp.server.entity.MasterComputerSearchDTO;
import com.pactera.asmp.server.entity.ResponseResult;
import com.pactera.asmp.server.pojo.MasterComputer;
import com.pactera.asmp.server.service.MasterComputerServiceImpl;
import com.pactera.asmp.server.service.TokenServiceImpl;
import com.pactera.asmp.server.utils.PageDataResult;

import com.pactera.asmp.server.vo.MasterComputerReq;
import com.pactera.asmp.server.vo.QueryMasterListCondition;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: asmp-server
 * @Package com.pactera.asmp.server.restfullapi
 * @ClassName: RestfullMasterComputerApi
 * @Description: RestfullMasterComputerApi
 * @Date 2020/06/30 9:53
 * @Author lvzhiqiang
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/v1")
public class RestfullMasterComputerApi {
    private static final Logger logger = LoggerFactory
            .getLogger(RestfullMasterComputerApi.class);

    @Autowired
    private MasterComputerServiceImpl mMasterComputerService;
    @Autowired
    private TokenServiceImpl mTokenService;

    @GetMapping(value = "/masterc", produces = "application/json;charset=UTF-8")
    public ResponseResult getMasterComputer(@RequestParam("masterComputerSearchDTO") String masterComputerSearchDTO) {
        logger.info("/api/v1/masterc getMasterComputer : MasterComputerSearchDTO:{}" , masterComputerSearchDTO);
        JSONObject resultJSON = (JSONObject) JSON.parse(masterComputerSearchDTO);
        MasterComputerSearchDTO data = JSON.toJavaObject(resultJSON, MasterComputerSearchDTO.class);
        int page = data.getPage();
        int limit = data.getLimit();
        ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
        PageDataResult pdr ;
        if (0 == page) {
            page = 1;
        }
        if (0 == limit) {
            limit = 10;
        }
        pdr = mMasterComputerService.get(data, page, limit);
        JSONObject result = new JSONObject();
        result.put("totals", pdr.getTotals());
        result.put("list", pdr.getList());
        responseResult.setData(result);
        return responseResult;
    }

    @GetMapping(value = "/masterc/list", produces = "application/json;charset=UTF-8")
    public ResponseResult getMasterComputer() {
        logger.info("/api/v1/masterc/list getMasterComputer");
        return mMasterComputerService.getMastercList();
    }

    @GetMapping(value = "/masterc/listByProjectId", produces = "application/json;charset=UTF-8")
    public ResponseResult<List<MasterComputer>> getMasterComputerByProjectId(@RequestParam("projectId") Integer projectId) {
        logger.info("/api/v1/masterc/listByProjectId : {}",projectId);
        return mMasterComputerService.getMastercListByProjectId(projectId);
    }

    @PutMapping(value = "/masterc", produces = "application/json;charset=UTF-8")
    public ResponseResult updateMasterComputer(@RequestBody MasterComputer masterComputer) {
        logger.info("/api/v1/masterc updateMasterComputer masterComputer :{}" , masterComputer);
        if (masterComputer == null || masterComputer.getId() == 0 || StringUtils.isEmpty(masterComputer.getCuid())) {
            return new ResponseResult(ResultCode.PARAM_IS_BLANK);
        }
        return mMasterComputerService.update(masterComputer) ? new ResponseResult(ResultCode.SUCCESS) : new ResponseResult(ResultCode.SYSTEM_INNER_ERROR);
    }

    @DeleteMapping(value = "/masterc", produces = "application/json;charset=UTF-8")
    public ResponseResult deleteMasterComputer(@RequestBody MasterComputerReq datas) {
        logger.debug("/api/v1/masterc deleteMasterComputer datas :{}" , datas);
        if (datas.getIds().isEmpty()) {
            return new ResponseResult(ResultCode.PARAM_IS_BLANK);
        }
        return mMasterComputerService.delete(datas.getIds()) ? new ResponseResult(ResultCode.SUCCESS) : new ResponseResult(ResultCode.SYSTEM_INNER_ERROR);
    }

    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        System.out.println(JSONObject.toJSONString(list));
    }

    /**
     * @api {POST} /api/v1/masterc/nickname updateMasterComputerNickName
     * @apiVersion 1.0.0
     * @apiGroup RestfullMasterComputerApi
     * @apiName updateMasterComputerNickName
     * @apiDescription 修改上位机昵称
     * @apiParam (请求体) {Number} id id
     * @apiParam (请求体) {String} nickName 昵称
     * @apiParamExample 请求体示例
     * {"nickName":"ezvy","id":5257}
     * @apiHeader {String} Authorization Bearer xxx
     * @apiHeaderExample {json} 请求头样例:
     * { "Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1dWlkIjoiNTAiLCJjaGFubmVsSWQiOjUsInJvbGVJZCI6IjIiLCJjcmVhdGVUaW1lIjoxNjE1MzU5MDgxNDY1LCJsb2NhbGhvc3QiOiJCQUIxOTAwMjE1Iiwic3ViIjoiYWRtaW4iLCJpc3MiOiIyZDBhNDI2MTY5MjYwYzA4NmIxNWU4MzlhYmEyMGUwOSIsImlhdCI6MTYxNTM1OTA4MSwiYXVkIjoicmVzdGFwaXVzZXIiLCJleHAiOjE2MTc5NTEwODEsIm5iZiI6MTYxNTM1OTA4MX0.BpUVtIE4kWyWKD8-b7lAv1611rv5N1U1q0VO4ijD0P0" }
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccessExample 响应结果示例
     * {"code":79,"data":{},"message":"l"}
     */
    @PostMapping(value = "/masterc/nickname", produces = "application/json;charset=UTF-8")
    public ResponseResult updateMasterComputerNickName(@RequestBody MasterComputer masterComputer) {
        logger.info("/api/v1/masterc/nickname masterComputer :{}" , masterComputer);
        return mMasterComputerService.updateNickName(masterComputer) >0  ? ResponseResult.defaultSuccess(null):ResponseResult.defaultFail(-1,"更新失败");
    }

    /**
     * 根据项目获取上位机列表
     * 
     * @param projectId
     * @return
     */
    @GetMapping("/masterc/mastercBindByProjectId")
    public ResponseResult mastercBindByProjectId(@RequestParam("projectId") Integer projectId) {
        logger.debug("/api/v1/masterc/mastercBindByProjectId projectId: {}", projectId);
        if (null == projectId) {
            return new ResponseResult(ResultCode.PARAM_IS_BLANK);
        }
        return ResponseResult.defaultSuccess(mMasterComputerService.mastercBindByProjectId(projectId));
    }


    /**
     * 根据项目获取绑定上位机列表
     * 
     * @param projectId
     * @return
     */
    @GetMapping("/masterc/listBindByProjectId")
    public ResponseResult listBindByProjectId(@RequestParam("projectId") Integer projectId) {
        logger.debug("/api/v1/masterc/listBindByProjectId projectId: {}", projectId);
        if (null == projectId) {
            return new ResponseResult(ResultCode.PARAM_IS_BLANK);
        }
        return ResponseResult.defaultSuccess(mMasterComputerService.listBindByProjectId(projectId, null));
    }
    //台架名，品牌，型号, status

    @PostMapping("/masterc/listBindByCondition")
    public ResponseResult listBindByCondition(@RequestBody QueryMasterListCondition condition) {
        logger.debug("/api/v1/masterc/listBindByCondition projectId: {}", condition.getProjectId());
        if (null == condition.getProjectId()) {
            return new ResponseResult(ResultCode.PARAM_IS_BLANK);
        }
        return ResponseResult.defaultSuccess(mMasterComputerService.listBindByCondition(condition));
    }

    /**
     * 根据项目获取上位机列表
     *
     * @param condition
     * @return
     */
    @PostMapping("/masterc/mastercBindByCondition")
    public ResponseResult mastercBindByCondition(@RequestBody QueryMasterListCondition condition) {
        logger.debug("/api/v1/masterc/mastercBindByCondition projectId: {}", condition.getProjectId());
        if (null == condition.getProjectId()) {
            return new ResponseResult(ResultCode.PARAM_IS_BLANK);
        }
        return ResponseResult.defaultSuccess(mMasterComputerService.mastercBindByCondition(condition));
    }
}
